package com.budweiser.commons.axis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import javax.xml.soap.*;
import java.io.IOException;

@Slf4j
public class SoapUtils {
	private static String PREFIX = "tem";
	private static String NAMESPACE_URI = "http://tempuri.org/";
	@Value("${bacco.k3url}")
	private String k3url;

	private static SOAPMessage createWGRKBillSaveRequest(String fdate, String departid, String supperid,
			String username, String memo, String body) throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		message.getMimeHeaders().addHeader("SOAPAction", "http://tempuri.org/WGRKBillSave");
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		Name WGRKBillSaveRequestName = envelope.createName("WGRKBillSave", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveRequestElement = message.getSOAPBody().addBodyElement(WGRKBillSaveRequestName);
		Name fdateName = envelope.createName("fdate", PREFIX, NAMESPACE_URI);
		SOAPElement msgElement = wGRKBillSaveRequestElement.addChildElement(fdateName);
		msgElement.setValue(fdate);
		Name departidName = envelope.createName("departid", PREFIX, NAMESPACE_URI);
		SOAPElement departidElement = wGRKBillSaveRequestElement.addChildElement(departidName);
		departidElement.setValue(departid);
		Name supperidName = envelope.createName("supperid", PREFIX, NAMESPACE_URI);
		SOAPElement supperidElement = wGRKBillSaveRequestElement.addChildElement(supperidName);
		supperidElement.setValue(supperid);
		Name usernameName = envelope.createName("username", PREFIX, NAMESPACE_URI);
		SOAPElement usernameElement = wGRKBillSaveRequestElement.addChildElement(usernameName);
		usernameElement.setValue(username);
		Name memoName = envelope.createName("memo", PREFIX, NAMESPACE_URI);
		SOAPElement memoElement = wGRKBillSaveRequestElement.addChildElement(memoName);
		memoElement.setValue(memo);
		Name bodyName = envelope.createName("body", PREFIX, NAMESPACE_URI);
		SOAPElement bodyElement = wGRKBillSaveRequestElement.addChildElement(bodyName);
		bodyElement.setValue(body);
		return message;
	}

	public static SoapResult callWGRKBillSave(String k3Url, String fdate, String departid, String supperid,
			String username, String memo, String body) throws SOAPException, IOException {
		SOAPMessage request = createWGRKBillSaveRequest(fdate, departid, supperid, username, memo, body);
		request.writeTo(System.out);
		SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
		SOAPMessage response = connection.call(request, k3Url);
		request.writeTo(System.out);
		if (!response.getSOAPBody().hasFault()) {
			log.info("外购入库单保存接口調用成功!{},{},{},{},{},{}", fdate, departid, supperid, username, memo, body);
			return writeResponse(response, "WGRKBillSave");

		} else {
			SOAPFault fault = response.getSOAPBody().getFault();
			return new SoapResult(fault.getFaultCode(), null, fault.getFaultString());
		}
	}

	private static SoapResult writeResponse(SOAPMessage message, String resultType) throws SOAPException {
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		Name WGRKBillSaveResponseName = envelope.createName(resultType + "Response", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveResponseElement = (SOAPBodyElement) message.getSOAPBody()
				.getChildElements(WGRKBillSaveResponseName).next();
		Name WGRKBillSaveResult = envelope.createName(resultType + "Result", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveResultElement = (SOAPBodyElement) wGRKBillSaveResponseElement
				.getChildElements(WGRKBillSaveResult).next();
		Name ResultCode = envelope.createName("ResultCode", PREFIX, NAMESPACE_URI);
		SOAPBodyElement resultCodeElement = (SOAPBodyElement) wGRKBillSaveResultElement.getChildElements(ResultCode)
				.next();
		String resultCodeValue = resultCodeElement.getFirstChild().getNodeValue();
		Name FbillNo = envelope.createName("FbillNo", PREFIX, NAMESPACE_URI);
		SOAPBodyElement fbillNoElement = (SOAPBodyElement) wGRKBillSaveResultElement.getChildElements(FbillNo).next();
		String fbillNoValue = null;
		if (fbillNoElement.getFirstChild() != null && !"".equals(fbillNoElement.getFirstChild())) {
			fbillNoValue = fbillNoElement.getFirstChild().getNodeValue();
		}
		Name Description = envelope.createName("Description", PREFIX, NAMESPACE_URI);
		SOAPBodyElement descriptionElement = (SOAPBodyElement) wGRKBillSaveResultElement.getChildElements(Description)
				.next();
		String descriptionValue = null;
		if (descriptionElement.getFirstChild() != null && !"".equals(descriptionElement.getFirstChild())) {
			descriptionValue = descriptionElement.getFirstChild().getNodeValue();
		}
		return new SoapResult(resultCodeValue, fbillNoValue, descriptionValue);
	}

	public static SoapResult callCPRKBillSave(String k3Url, String fdate, String departid, String username, String memo,
			String body) {
		try {
			SOAPMessage request = createCPRKBillSaveRequst(fdate, departid, username, memo, body);
			SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
			SOAPMessage response = connection.call(request, k3Url);
			if (!response.getSOAPBody().hasFault()) {
				log.info("产品入库保存接口調用成功!{},{},{},{}", fdate, departid, username, memo, body);
				return writeResponse(response, "CPRKBillSave");
			} else {
				SOAPFault fault = response.getSOAPBody().getFault();
				return new SoapResult(fault.getFaultCode(), null, fault.getFaultString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static SOAPMessage createCPRKBillSaveRequst(String fdate, String departid, String username, String memo,
			String body) throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		message.getMimeHeaders().addHeader("SOAPAction", "http://tempuri.org/CPRKBillSave");
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		Name WGRKBillSaveRequestName = envelope.createName("CPRKBillSave", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveRequestElement = message.getSOAPBody().addBodyElement(WGRKBillSaveRequestName);
		Name fdateName = envelope.createName("fdate", PREFIX, NAMESPACE_URI);
		SOAPElement msgElement = wGRKBillSaveRequestElement.addChildElement(fdateName);
		msgElement.setValue(fdate);
		Name departidName = envelope.createName("departid", PREFIX, NAMESPACE_URI);
		SOAPElement departidElement = wGRKBillSaveRequestElement.addChildElement(departidName);
		departidElement.setValue(departid);
		Name usernameName = envelope.createName("username", PREFIX, NAMESPACE_URI);
		SOAPElement usernameElement = wGRKBillSaveRequestElement.addChildElement(usernameName);
		usernameElement.setValue(username);
		Name memoName = envelope.createName("memo", PREFIX, NAMESPACE_URI);
		SOAPElement memoElement = wGRKBillSaveRequestElement.addChildElement(memoName);
		memoElement.setValue(memo);
		Name bodyName = envelope.createName("body", PREFIX, NAMESPACE_URI);
		SOAPElement bodyElement = wGRKBillSaveRequestElement.addChildElement(bodyName);
		bodyElement.setValue(body);
		return message;
	}

	public static SoapResult callDBDBillSave(String k3Url, String fdate, String ys, String bg, String body)
			throws SOAPException, IOException {
		SOAPMessage request = callDBDBillSaveRequest(fdate, ys, bg, body);
		request.writeTo(System.out);
		SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
		SOAPMessage response = connection.call(request, k3Url);
		if (!response.getSOAPBody().hasFault()) {
			log.info("调拨单保存接口調用成功!{},{},{},{}", fdate, ys, bg, body);
			return writeResponse(response, "DBDBillSave");

		} else {
			SOAPFault fault = response.getSOAPBody().getFault();
			return new SoapResult(fault.getFaultCode(), null, fault.getFaultString());
		}
	}

	private static SOAPMessage callDBDBillSaveRequest(String fdate, String ys, String bg, String body)
			throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		message.getMimeHeaders().addHeader("SOAPAction", "http://tempuri.org/DBDBillSave");
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		Name WGRKBillSaveRequestName = envelope.createName("DBDBillSave", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveRequestElement = message.getSOAPBody().addBodyElement(WGRKBillSaveRequestName);
		Name fdateName = envelope.createName("fdate", PREFIX, NAMESPACE_URI);
		SOAPElement msgElement = wGRKBillSaveRequestElement.addChildElement(fdateName);
		msgElement.setValue(fdate);

		Name ysName = envelope.createName("ys", PREFIX, NAMESPACE_URI);
		SOAPElement ysElement = wGRKBillSaveRequestElement.addChildElement(ysName);
		ysElement.setValue(ys);
		Name bgName = envelope.createName("bg", PREFIX, NAMESPACE_URI);
		SOAPElement bgElement = wGRKBillSaveRequestElement.addChildElement(bgName);
		bgElement.setValue(bg);
		Name bodyName = envelope.createName("body", PREFIX, NAMESPACE_URI);
		SOAPElement bodyElement = wGRKBillSaveRequestElement.addChildElement(bodyName);
		bodyElement.setValue(body);
		return message;
	}

	public static SoapResult callPickBillSave(String k3Url, String fdate, String lingliao, String faliao, String body)
			throws SOAPException, IOException {
		SOAPMessage request = callPickBillSaveRequest(fdate, lingliao, faliao, body);
		request.writeTo(System.out);
		SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
		SOAPMessage response = connection.call(request, k3Url);
		if (!response.getSOAPBody().hasFault()) {
			log.info("领料单保存接口調用成功!{},{},{},{}", fdate, lingliao, faliao, body);
			return writeResponse(response, "PickBillSave");

		} else {
			SOAPFault fault = response.getSOAPBody().getFault();
			return new SoapResult(fault.getFaultCode(), null, fault.getFaultString());
		}
	}

	private static SOAPMessage callPickBillSaveRequest(String fdate, String lingliao, String faliao, String body)
			throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		message.getMimeHeaders().addHeader("SOAPAction", "http://tempuri.org/PickBillSave");
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		Name WGRKBillSaveRequestName = envelope.createName("PickBillSave", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveRequestElement = message.getSOAPBody().addBodyElement(WGRKBillSaveRequestName);
		Name fdateName = envelope.createName("fdate", PREFIX, NAMESPACE_URI);
		SOAPElement msgElement = wGRKBillSaveRequestElement.addChildElement(fdateName);
		msgElement.setValue(fdate);

		Name lingliaoName = envelope.createName("lingliao", PREFIX, NAMESPACE_URI);
		SOAPElement lingliaoElement = wGRKBillSaveRequestElement.addChildElement(lingliaoName);
		lingliaoElement.setValue(lingliao);
		Name faliaoName = envelope.createName("faliao", PREFIX, NAMESPACE_URI);
		SOAPElement faliaoElement = wGRKBillSaveRequestElement.addChildElement(faliaoName);
		faliaoElement.setValue(faliao);
		Name bodyName = envelope.createName("body", PREFIX, NAMESPACE_URI);
		SOAPElement bodyElement = wGRKBillSaveRequestElement.addChildElement(bodyName);
		bodyElement.setValue(body);
		return message;
	}

	public static SoapResult callSalOutSave(String k3Url, String fdate, String fcustId, String fdepartId,
			String username, String body) throws SOAPException, IOException {
		SOAPMessage request = callSalOutSaveRequest(fdate, fcustId, fdepartId, username, body);
		request.writeTo(System.out);
		SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
		SOAPMessage response = connection.call(request, k3Url);
		if (!response.getSOAPBody().hasFault()) {
			log.info("销售出库单保存接口調用成功!{},{},{},{},{},{},{},{}", fdate, fcustId, fdepartId, username, body);
			return writeResponse(response, "SalOutSave");

		} else {
			SOAPFault fault = response.getSOAPBody().getFault();
			return new SoapResult(fault.getFaultCode(), null, fault.getFaultString());
		}
	}

	private static SOAPMessage callSalOutSaveRequest(String fdate, String fcustId, String fdepartId, String username,
			String body) throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		message.getMimeHeaders().addHeader("SOAPAction", "http://tempuri.org/SalOutSave");
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		Name WGRKBillSaveRequestName = envelope.createName("SalOutSave", PREFIX, NAMESPACE_URI);
		SOAPBodyElement wGRKBillSaveRequestElement = message.getSOAPBody().addBodyElement(WGRKBillSaveRequestName);
		Name fdateName = envelope.createName("fdate", PREFIX, NAMESPACE_URI);
		SOAPElement msgElement = wGRKBillSaveRequestElement.addChildElement(fdateName);
		msgElement.setValue(fdate);

		Name fcustIdName = envelope.createName("fcustId", PREFIX, NAMESPACE_URI);
		SOAPElement fcustIdElement = wGRKBillSaveRequestElement.addChildElement(fcustIdName);
		fcustIdElement.setValue(fcustId);
		Name faliaoName = envelope.createName("fdepartId", PREFIX, NAMESPACE_URI);
		SOAPElement fdepartIdElement = wGRKBillSaveRequestElement.addChildElement(faliaoName);
		fdepartIdElement.setValue(fdepartId);
		Name usernameName = envelope.createName("username", PREFIX, NAMESPACE_URI);
		SOAPElement fusernameElement = wGRKBillSaveRequestElement.addChildElement(usernameName);
		fusernameElement.setValue(username);
		Name bodyName = envelope.createName("body", PREFIX, NAMESPACE_URI);
		SOAPElement bodyElement = wGRKBillSaveRequestElement.addChildElement(bodyName);
		bodyElement.setValue(body);
		return message;
	}
}
